package com.kongwg3.connection.junit;

import com.kongwg3.connection.util.JDBCUtil;
import com.kongwg1.dao.CustomerDAOImpl;
import com.kongwg1.dao.bean.Customer;
import org.junit.Test;

import java.sql.Connection;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * @author kongwg
 * @create 2021-01-09 16:38
 */
public class CustomerDAOImplTest {

    CustomerDAOImpl dao = new CustomerDAOImpl();

    @Test
    public void insert() {
        Connection conn = null;
        try {
            conn = JDBCUtil.getConnection();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            java.util.Date date = sdf.parse("2020-02-02");

            dao.insert(conn, new Customer(30, "张三1", "zhangsan1@qq.com", new Date(date.getTime())));
            System.out.println("添加成功");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtil.closeResource(conn, null);
        }

    }

    @Test
    public void deleteById() {
        Connection conn = null;
        try {
            conn = JDBCUtil.getConnection();
            dao.deleteById(conn, 13);

            System.out.println("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtil.closeResource(conn, null);
        }


    }

    @Test
    public void update() {
        Connection conn = null;
        try {
            conn = JDBCUtil.getConnection();
            Customer cus = new Customer(12, "赵敏", "zhaoming@gmail.com", new Date(2323323232L));
            dao.update(conn, cus);

            System.out.println("修改成功");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtil.closeResource(conn, null);
        }

    }

    @Test
    public void getCustomerById() {
        Connection conn = null;
        try {
            conn = JDBCUtil.getConnection1();
            Customer cust = dao.getCustomerById(conn, 1);
            System.out.println(cust);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtil.closeResource(conn, null);
        }

    }

    @Test
    public void getAll() {
        Connection conn = null;
        try {
            conn = JDBCUtil.getConnection1();
            List<Customer> list = dao.getAll(conn);
            list.forEach(System.out::println);


        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtil.closeResource(conn, null);
        }

    }

    @Test
    public void getCount() {
        Connection conn = null;
        try {
            conn = JDBCUtil.getConnection();
            long count = dao.getCount(conn);
            System.out.println(count);


        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtil.closeResource(conn, null);
        }

    }

    @Test
    public void getMaxBirth() {
        Connection conn = null;
        try {
            conn = JDBCUtil.getConnection();
            Date maxBirth = dao.getMaxBirth(conn);
            System.out.println(maxBirth);

            System.out.println("添加成功");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtil.closeResource(conn, null);
        }

    }
}